package tgc.edu.library.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.library.custom.CommonController;
import tgc.edu.library.custom.EasyuiResult;
import tgc.edu.library.custom.LogUtils;
import tgc.edu.library.custom.TimeUtils;
import tgc.edu.library.entity.Book;
import tgc.edu.library.entity.BorrowRecord;
import tgc.edu.library.entity.Reader;
import tgc.edu.library.entity.SysUser;
import tgc.edu.library.security.UserUtils;
import tgc.edu.library.service.BookService;
import tgc.edu.library.service.BorrowRecordService;
import tgc.edu.library.service.ReaderService;
import tgc.edu.library.web.form.BorrowRecordForm;

@Controller
@RequestMapping(value="/borrowRecord")
public class BorrowRecordController extends CommonController<BorrowRecord, Integer, BorrowRecordForm>{
	@Autowired
	private BorrowRecordService borrowRecordService;
	@Autowired
	private UserUtils user;
	@Autowired
	private BookService bookService;
	@Autowired
	private ReaderService readerService;
	@Autowired
	private LogUtils logUtils;
	
	@RequestMapping(value="/recordAll")
	public Object recordAll(ModelMap map) {
		SysUser user2 = user.getUser();
		System.err.println(user2.getId());
		Reader reader = readerService.findById(user2.getId());
		List<BorrowRecord> list = borrowRecordService.findByReaderAndStatus(reader,"已同意");
		map.put("list", list);
		return "user/recordAll";
	}
	
	@RequestMapping(value="/edit1")
	@ResponseBody
	public Object edit(Integer id) {
		SysUser user2 = user.getUser();
		BorrowRecord record = new BorrowRecord();
		String stringDate = TimeUtils.getStringDate();
		record = borrowRecordService.findById(id);
		record.setStatus("已同意");
		record.setOperation(user2.getUsername());
		record.setBorrowtime(stringDate);
		logUtils.log(user2.getUsername(), "同意借书");
		borrowRecordService.save(record);
		return new EasyuiResult("已同意");
	}
	
	@RequestMapping(value="/edit2")
	@ResponseBody
	public Object edi2(Integer id) {
		SysUser user2 = user.getUser();
		BorrowRecord record = new BorrowRecord();
		record = borrowRecordService.findById(id);
		record.setStatus("已拒绝");
		record.setOperation(user2.getUsername());
		logUtils.log(user2.getUsername(), "拒绝借书");
		borrowRecordService.save(record);
		return new EasyuiResult("已拒绝");
	}
	
	@RequestMapping(value="/borrow")
	@ResponseBody
	public Object borrow(Integer bookid) {
		SysUser user2 = user.getUser();
		Book book = bookService.findById(bookid);
		BorrowRecord record = new BorrowRecord();
		String stringDate = TimeUtils.getStringDate();
		if (book.getQuantity()==0) {
			return new EasyuiResult(false,"库存不足！");
		}
		Integer quantity = book.getQuantity()-1;
		book.setQuantity(quantity);
		record.setReader(readerService.findById(user2.getId()));
		record.setBook(book);
		record.setApplytime(stringDate);
		record.setStatus("申请中");
		borrowRecordService.save(record);
		return new EasyuiResult("申请成功！");
	}
	
	@RequestMapping(value="/givebook")
	@ResponseBody
	public Object givebook(Integer recordid) {
		String stringDate = TimeUtils.getStringDate();
		BorrowRecord record = borrowRecordService.findById(recordid);
		Book book = bookService.findById(record.getBook().getId());
		Integer quantity = book.getQuantity()+1;
		book.setQuantity(quantity);
		record.setGivetime(stringDate);
		record.setStatus("已归还");
		borrowRecordService.save(record);
		return new EasyuiResult("还书成功！");
	}
}
